<template>
  <div>
    <div class="ly-filter">
      <el-input placeholder="请输入要搜索的药品名称" v-model="query.drugName"></el-input>
      <el-input placeholder="请输入要搜索的拼音助记码" v-model="query.mnemonicCode"></el-input>
      <el-button type="primary" @click="search">搜索</el-button>
      <el-button type="primary" @click="add">添加</el-button>
    </div>
    <el-table ref="wytable" :data="tableData">
      <el-table-column type="selection" width="55" />
      <el-table-column label="药品编码" prop="drugCode"></el-table-column>
      <el-table-column label="药品名称" prop="drugName"></el-table-column>
      <el-table-column label="药品规格" prop="drugFormat"></el-table-column>
      <el-table-column label="药品单价" prop="drugPrice"></el-table-column>
      <el-table-column label="包装单位" prop="drugUnit"></el-table-column>
      <el-table-column label="生产厂家" prop="manufacturer"></el-table-column>
    </el-table>
  </div>
</template>

<script setup>
import { reactive, ref } from 'vue'
import { get } from '@/utils/axios'
import { ElMessage } from 'element-plus'
const emits = defineEmits(['addRow']) 
const wytable = ref()
const query = reactive({
  mnemonicCode: '',
  drugName: ''
})
const tableData = ref([])
function search () {
  if (query.drugName.trim() || query.mnemonicCode.trim()) {
    getTableData()
  } else {
    ElMessage.error('至少要输入一个条件')
  }
}
function getTableData () {
  get('/drugInfo/list', query, content => {
    tableData.value = content
  })
}
function add () {
  const rows = wytable.value.getSelectionRows() // 获取表格中被选中的行数据
  for (const i in rows) {
    const row = {
      id: rows[i].id,
      drugName: rows[i].drugName,
      drugFormat: rows[i].drugFormat,
      drugPrice: rows[i].drugPrice,
      drugUsage: '',
      drugNumber: 1
    }
    emits('addRow', row)  //用于触发父组件的addRow的函数
  }
}
</script>

<style scoped lang="scss">
</style>
